#include "FSearch.h"


FSearch::FSearch(void)
{
}

FSearch::~FSearch(void)
{
}

int
FSearch::find(std::vector<double> const &trace, double d)
{
	int hi = trace.size() - 1;
	int lo = 0;

	while(lo < hi - 1) {
		int mi = (lo + hi) / 2;
		double dmi = trace[mi];
		
		while(this->isNaN(dmi) && lo < mi) {
			--mi;
			dmi = trace[mi];
		}

		if(mi == lo) {
			lo = (lo + hi) / 2;
			continue;
		}

		if(isEqual(dmi, d))
			return mi;
		if (d < dmi) {
			hi = mi;
		} else {
			lo = mi;
		}
	}

	if(!isNaN(trace[lo]) && !isNaN(trace[hi]))
		return lo;

	return -1; // not found
}
